﻿package com.vicentediaz.view.menu
{
    import com.greensock.*;
    import com.myp.flow.*;
    import com.myp.mystage.*;
    import com.myp.soundtrack.*;
    import com.vicentediaz.data.*;
    import com.vicentediaz.flow.*;
    import com.vicentediaz.view.about.*;
    import com.vicentediaz.view.contact.*;
    import com.vicentediaz.view.imageselector.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;

    public class MainMenu extends Sprite
    {
        private var buttons:Sprite;
        private var toolButtons:ToolButtons;
        private var waveButton:UtilButtonWave;
        private var base:MainMenuBase;
        private var currentPanel:DisplayObject;
        private var utilButtons:Sprite;
        private var imageSelector:ImageSelector;
        private var aboutButton:MainMenuButton;
        private var miniLogo:MiniLogo;
        private var social:SocialButtons;
        private var flow:Flow;
        private var imageFlow:Flow;
        private var contactButton:MainMenuButton;
        private var fullScreenButton:UtilButtonFullScreen;
        private var blogButton:MainMenuButton;
        private static const OPEN_HEIGHT:uint = 256;
        private static const SEMI_OPEN_HEIGHT:uint = 125;
        private static const CLOSE_HEIGHT:uint = 45;
        private static const UP_TIME:Number = 0.5;
        private static const BUTTONS_POS:uint = 25;
        private static const IMAGE_SELECTOR_POS:uint = 40;

        public function MainMenu()
        {
            this.flow = Flow.level(MainFlow.LEVEL);
            this.flow.addEventListener(FlowEvent.CHANGE, this.onFlowChange, false, 0, true);
            this.flow.addEventListener(FlowEvent.REQUEST, this.onFlowRequest, false, 0, true);
            this.imageFlow = Flow.level(ImageFlow.LEVEL);
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage, false, 0, true);
            return;
        }// end function

        private function accept() : void
        {
            this.flow.accept("MainMenu");
            return;
        }// end function

        private function showClose() : void
        {
            TweenMax.to(this.base, UP_TIME, {height:CLOSE_HEIGHT});
            TweenMax.to(this.buttons, UP_TIME, {y:-BUTTONS_POS});
            TweenMax.to(this.utilButtons, UP_TIME, {y:-BUTTONS_POS - 4});
            TweenMax.to(this.imageSelector, UP_TIME, {y:-IMAGE_SELECTOR_POS});
            Sounds.getInstance().playById("PAPER", 0.05, 0);
            return;
        }// end function

        private function onFlowRequest(param1:FlowEvent) : void
        {
            switch(this.flow.currentSection)
            {
                case MainFlow.HOME:
                {
                    this.flow.accept(param1.currentSection);
                    break;
                }// end case
                case MainFlow.CONTACT:
                {
                    this.hidePanel();
                    if (param1.nextSection != MainFlow.ABOUT)
                    {
                        this.showClose();
                    }// end if
                    this.imageFlow.request(null, "MainMenu");
                    break;
                }// end case
                case MainFlow.ABOUT:
                {
                    this.hidePanel();
                    if (param1.nextSection != MainFlow.CONTACT)
                    {
                        this.showClose();
                    }// end if
                    this.imageFlow.request(null, "MainMenu");
                    break;
                }// end case
                case MainFlow.IMAGEN:
                {
                    this.flow.accept("MainMenu");
                    break;
                }// end case
                default:
                {
                    this.flow.accept("MainMenu");
                    break;
                }// end default
            }// end switch
            return;
        }// end function

        private function showAbout() : DisplayObject
        {
            var _loc_1:* = new About();
            addChild(_loc_1);
            TweenMax.to(_loc_1, UP_TIME, {y:-_loc_1.height});
            Sounds.getInstance().playById("PAPER", 0.05, 0);
            return _loc_1;
        }// end function

        private function onFullScreenButtonClick(param1:MouseEvent) : void
        {
            if (MyStage.getInstance().stage.displayState == StageDisplayState.NORMAL)
            {
                MyStage.getInstance().stage.displayState = StageDisplayState.FULL_SCREEN;
            }
            else
            {
                MyStage.getInstance().stage.displayState = StageDisplayState.NORMAL;
            }// end else if
            return;
        }// end function

        private function onBlogButtonClick(param1:MouseEvent) : void
        {
            navigateToURL(new URLRequest(Config.getInstance().getByValue("BLOG")), "_blank");
            return;
        }// end function

        private function hidePanel() : void
        {
            Sounds.getInstance().playById("PAPER", 0.05, 0);
            TweenMax.to(this.currentPanel, UP_TIME, {y:0, onComplete:this.accept});
            return;
        }// end function

        private function onWaveButtonClick(param1:MouseEvent) : void
        {
            Soundtrack.getInstance().togglePause();
            if (Soundtrack.getInstance().paused)
            {
                this.waveButton.fadeIcon(0.3);
            }
            else
            {
                this.waveButton.fadeIcon(1);
            }// end else if
            return;
        }// end function

        private function onResize(param1:Event) : void
        {
            this.base.width = MyStage.getInstance().width;
            this.utilButtons.x = MyStage.getInstance().width - 20;
            return;
        }// end function

        private function showContact() : DisplayObject
        {
            var _loc_1:* = new Contact();
            addChild(_loc_1);
            TweenMax.to(_loc_1, UP_TIME, {y:-_loc_1.height});
            Sounds.getInstance().playById("PAPER", 0.05, 0);
            return _loc_1;
        }// end function

        private function showOpen() : void
        {
            TweenMax.to(this.base, UP_TIME, {height:OPEN_HEIGHT});
            TweenMax.to(this.buttons, UP_TIME, {y:-OPEN_HEIGHT + (CLOSE_HEIGHT - BUTTONS_POS)});
            TweenMax.to(this.utilButtons, UP_TIME, {y:-OPEN_HEIGHT + (CLOSE_HEIGHT - BUTTONS_POS - 4)});
            TweenMax.to(this.imageSelector, UP_TIME, {y:-OPEN_HEIGHT + (CLOSE_HEIGHT - IMAGE_SELECTOR_POS)});
            Sounds.getInstance().playById("PAPER", 0.05, 0);
            return;
        }// end function

        private function onAboutButtonClick(param1:MouseEvent) : void
        {
            this.flow.request(MainFlow.ABOUT, "MainMenuButton");
            return;
        }// end function

        private function onAddedToStage(param1:Event) : void
        {
            MyStage.getInstance().stage.addEventListener(Event.RESIZE, this.onResize, false, 0, true);
            this.start();
            this.onResize(null);
            this.onFlowChange(null);
            return;
        }// end function

        private function onContactButtonClick(param1:MouseEvent) : void
        {
            this.flow.request(MainFlow.CONTACT, "MainMenuButton");
            return;
        }// end function

        private function start() : void
        {
            this.buttons = new Sprite();
            this.utilButtons = new Sprite();
            this.waveButton = new UtilButtonWave();
            this.fullScreenButton = new UtilButtonFullScreen();
            this.waveButton.x = -40;
            this.fullScreenButton.x = this.waveButton.x + this.waveButton.width + 1;
            this.social = new SocialButtons();
            this.social.x = -144;
            this.toolButtons = new ToolButtons();
            this.toolButtons.x = -82;
            this.utilButtons.addChild(this.waveButton);
            this.utilButtons.addChild(this.fullScreenButton);
            this.utilButtons.addChild(this.social);
            this.utilButtons.addChild(this.toolButtons);
            this.base = new MainMenuBase(CLOSE_HEIGHT);
            addChild(this.base);
            this.miniLogo = new MiniLogo();
            this.miniLogo.x = 20;
            this.buttons.addChild(this.miniLogo);
            this.contactButton = new MainMenuButton("_contact");
            this.aboutButton = new MainMenuButton("_about");
            this.blogButton = new MainMenuButton("_blog");
            var _loc_1:Number;
            this.contactButton.x = _loc_1;
            _loc_1 = _loc_1 + (this.contactButton.width + 10);
            this.aboutButton.x = _loc_1;
            _loc_1 = _loc_1 + (this.aboutButton.width + 10);
            this.blogButton.x = _loc_1;
            _loc_1 = _loc_1 + (this.blogButton.width + 10);
            this.buttons.addChild(this.contactButton);
            this.buttons.addChild(this.aboutButton);
            this.buttons.addChild(this.blogButton);
            this.buttons.y = -BUTTONS_POS;
            this.utilButtons.y = -BUTTONS_POS - 4;
            this.imageSelector = new ImageSelector();
            this.imageSelector.y = -IMAGE_SELECTOR_POS;
            addChild(this.imageSelector);
            this.contactButton.addEventListener(MouseEvent.CLICK, this.onContactButtonClick, false, 0, true);
            this.aboutButton.addEventListener(MouseEvent.CLICK, this.onAboutButtonClick, false, 0, true);
            this.blogButton.addEventListener(MouseEvent.CLICK, this.onBlogButtonClick, false, 0, true);
            this.waveButton.addEventListener(MouseEvent.CLICK, this.onWaveButtonClick, false, 0, true);
            this.fullScreenButton.addEventListener(MouseEvent.CLICK, this.onFullScreenButtonClick, false, 0, true);
            addChild(this.buttons);
            addChild(this.utilButtons);
            return;
        }// end function

        private function onFlowChange(param1:FlowEvent) : void
        {
            if (this.currentPanel && contains(this.currentPanel))
            {
                removeChild(this.currentPanel);
                this.currentPanel = null;
            }// end if
            switch(this.flow.currentSection)
            {
                case MainFlow.HOME:
                {
                    break;
                }// end case
                case MainFlow.CONTACT:
                {
                    this.showOpen();
                    this.currentPanel = this.showContact();
                    this.imageFlow.request(ImageFlow.NONE, "MainMenu");
                    break;
                }// end case
                case MainFlow.ABOUT:
                {
                    this.showOpen();
                    this.currentPanel = this.showAbout();
                    this.imageFlow.request(ImageFlow.NONE, "MainMenu");
                    break;
                }// end case
                case MainFlow.IMAGEN:
                {
                    break;
                }// end case
                default:
                {
                    break;
                }// end default
            }// end switch
            return;
        }// end function

    }
}
